<template>
	<view>
		<view class="header">
			<uni-section title="控制清除/取消按钮" subTitle="使用 clearButton 属性设置清除按钮" type="line">
				<uni-search-bar radius="5" placeholder="请输入用户名关键词" clearButton="always" cancelButton="always"
					@confirm="search" @cancel="cancel" />
			</uni-section>
		</view>
		<view class="body" v-if="isFind">
			<view class="Box" v-if="userRolesList.length!=0">
				<view class="simpleInfo" v-for="item,index in userRolesList" :key="index">
					<view class="UserInfo">
						<view class="Avatar">
							<!-- <image :src="'http://cdn.gdxywsw.club/' + avatar" /> -->
							<image :src="'http://cdn.gdxywsw.club/' + item.avatar"></image>
						</view>
						<view>
							<view class="Nickname">{{item.nickname}}</view>
							<view class="role">
								<!-- <uni-tag v-for="item in item.roles" :key="index" :inverted="true" :text="item"
									type="warning" size="mini" /> -->
								<uni-tag :key="index" :inverted="true" :text="item.role" type="warning" size="mini" />
							</view>
						</view>
					</view>
					<view class="Buttons">
						<!-- <button type="primary" size="mini">设置</button>
						<button type="warn" size="mini">撤销</button> -->
						<view class="setRoles">
							<view class="text">
								切换角色
							</view>
							<uni-data-select v-model="setValue[index]" :localdata="setRange[index]" :clear="false"
								@change="((val)=>{change(val,index,setRange[index],item.uid,item.role)})">
							</uni-data-select>
						</view>

					</view>
				</view>
			</view>
			<view class="empty" v-else>
				没有此用户
			</view>
		</view>
		<view>
			<!-- 提示信息弹窗 -->
			<uni-popup ref="message" type="message">
				<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: '',
				userRolesList: [],
				isFind: false,
				setValue: [],
				setRange: [

				],
				AllRolesList: [],
				msgType: '',
				messageText: '',
				myRid: '',
				myRole: ''
			};
		},
		onLoad(options) {
			console.log(options);
			this.myRole = options.myRole
			if (options.myRole == '超级管理员') {
				this.myRid = 9
			} else if (options.myRole == '管理员') {
				this.myRid = 8
			} else if (options.myRole == '审查员') {
				this.myRid = 7
			} else if (options.myRole == '普通用户') {
				this.myRid = 2
			} else if (options.myRole == '封禁用户') {
				this.myRole = 1
			}
		},
		mounted() {
			this.getAllRolesList()
		},

		methods: {
			change(val, index, role, uid, otherRole) {

				if (this.myRid <= role[val].rid || otherRole == '超级管理员' || otherRole == this.myRole) {
					this.msgType = 'info'
					this.messageText = '您的权限不够设置该级别'
					this.$refs.message.open()
				} else {
					this.changeUserRole(uid, role[val].rid, index)
				}

			},
			search(res) {
				this.keyword = res.value
				this.isFind = true
				// this.range = []
				this.getSomeUsersList()
			},
			//查询用户并返回角色
			async getSomeUsersList() {
				this.value = []
				this.userRolesList = []
				let range = [{
						value: 0,
						text: '普通用户',
						rid: 2
					},
					{
						value: 1,
						text: '封禁用户',
						rid: 1
					},
					{
						value: 2,
						text: '审核员',
						rid: 7
					},
					{
						value: 3,
						text: '管理员',
						rid: 8
					}
				]
				let {
					data: res
				} = await uni.$http.get('/admins/listSomeUsers/' + this.keyword)
				if (res.code == 2000) {
					this.userRolesList = res.data
					this.getValue()
					this.userRolesList.forEach((item, index) => {
						this.setRange.push(range)
					})
					// this.compareRole()
				} else {
					uni.showToast({
						title: '查询出错',
						duration: 1500,
						icon: 'error'
					})
				}
			},
			//返回所有角色
			async getAllRolesList() {
				let {
					data: res
				} = await uni.$http.get('/admins/listAllRoles')
				if (res.code == 2000) {
					res.data.forEach((item, index) => {
						if (item.name == '超级管理员') {
							res.data.splice(index, 1)
						}
					})
					this.AllRolesList = res.data
				}
			},

			// 刷新角色权限
			async refresh(uid, index) {

				let {
					data: res
				} = await uni.$http.get('/admins/getUserRoleName/' + uid)
				if (res.code == 2000) {
					this.userRolesList[index].role = res.data
				}

			},


			//切换权限
			async changeUserRole(uid, rid, index) {
				let {
					data: res
				} = await uni.$http.get('/admins/changeUserRole/' + uid + '/' + rid)
				if (res.code == 2000) {
					this.msgType = 'success'
					this.messageText = '权限设置成功'
					this.$refs.message.open()
					this.refresh(uid, index)
				} else {
					this.msgType = 'error'
					this.messageText = '权限设置失败'
					this.$refs.message.open()
				}
			},

			getValue() {
				let obj = {}
				for (let i in this.userRolesList) {
					this.setValue.push(obj)
				}
			},
			/*后续开发
			// compareRole() {
			// 	let obj = {}

			// 	this.userRolesList.forEach((item, index) => {
			// 		let arr = Array.from(new Set([...item.roles, ...this.AllRolesList]))
			// 		arr.forEach((item, index) => {
			// 			if (item.value != '超级管理员' && item.value != '管理员') {
			// 				arr.splice(index, 1)
			// 			}
			// 		})
			// 		let newArr = arr.map((item, index) => {
			// 			console.log(item, '259');
			// 			return {
			// 				value: index,
			// 				text: item.name
			// 			}
			// 		})
			// 		console.log(newArr, 'newArr');
			// 		this.setRange.push(newArr)
			// 	})
			// 	// console.log(arr, 'arr');
			// },
			
			*/
		},
		onBackPress() {
			// #ifdef APP-PLUS
			plus.key.hideSoftKeybord();
			// #endif
		}

	}
</script>

<style lang="less">
	.Box {

		.simpleInfo {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin: 5px 8px;
			padding: 5px;
			border-radius: 5px;
			background-color: rgb(255, 255, 255);

			.UserInfo {
				display: flex;

				.Avatar image {
					width: 40px;
					height: 40px;
					border-radius: 40px;
					border: 1px solid #e1e4e6;
				}

				.Nickname {
					margin-top: 2px;
					margin-left: 5px;
					font-weight: bolder;
				}

				.Description {
					margin-top: 3px;
					font-size: 13px;
					margin-left: 5px;
				}
			}

			.Buttons {
				display: flex;

				.setRoles {
					display: flex;
					align-items: center;

					.text {
						font-size: 14px;
						color: #666;
						margin-right: 4px;
					}
				}
			}
		}

	}




	.empty {
		display: flex;
		justify-content: center;
		align-items: flex-end;
		font-size: 14px;
		color: #dedede;
		width: 100%;
		height: 300px;
		background: url('@/static/images/empty-image-default.png') no-repeat;
		background-size: 50%;
		background-position: 50% 50%;
	}


	.search-result {
		padding-top: 10px;
		padding-bottom: 20px;
		text-align: center;
	}

	.search-result-text {
		text-align: center;
		font-size: 14px;
		color: #666;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		padding: 0px;
	}

	.uni-mt-10 {
		margin-top: 10px;
	}
</style>
